/****************************************************************
*****************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;

cap log close;
clear all;
drop _all;
set more 1;
pause on;

log using log_plot_health.log, replace;

global gpicsdir "pics_health";
global gdtadir ".";

*do do_globals.do;

***********************************************;
**  health transition;
* at 50, draw according to the distribution;
* from 51, according to the transition prob;
************************************************;
use dta_MSM_Indiv.dta if t>=50, clear;
gen s_hs4exce=(ihealth==1);
gen s_hs4good=(ihealth==2);
gen s_hs4bad=(ihealth==3);
gen s_hs4disab=(ihealth==4);

sum s_hs4disab if t>=55 & t<=64;
sum s_hs4disab if t>=55 & t<64;

pause;
*cap log close;
*stop;


preserve;  
   collapse (mean) s_hs4exce s_hs4good s_hs4bad s_hs4disab, by(t); 
   sort t;
   save ${gpicsdir}/dta_plot_health.dta, replace;
restore;

sort id t;
by id: gen preh4 = ihealth[_n-1];
drop if t==50;
replace t = t - 1;
collapse (mean) s_hs4exce s_hs4good s_hs4bad s_hs4disab, by(t preh4); 
sort t preh4;
drop if preh4==4;
reshape wide s_hs4exce s_hs4good s_hs4bad s_hs4disab, i(t) j(preh4);
foreach iv in exce good bad disab {;
    ren s_hs4`iv'1 s_hs4_exce2`iv';
    ren s_hs4`iv'2 s_hs4_good2`iv';
    ren s_hs4`iv'3 s_hs4_bad2`iv';
};
sort t;
merge 1:1 t using ${gpicsdir}/dta_plot_health.dta, nogenerate;
sort t;
ren t age;
merge 1:1 age using dta_12_health_sim_hs.dta, nogenerate;
ren age t;
sort t;
save ${gpicsdir}/dta_plot_health.dta, replace;
 
*local lvdhs "../../bpfcodes_com/data";
local lvdhs ".";

infile t 
        hs4exce_hat hs4good_hat hs4bad_hat hs4disab_hat
        hs4_exce2exce_hat hs4_exce2good_hat hs4_exce2bad_hat hs4_exce2disab_hat 
        hs4_good2exce_hat hs4_good2good_hat hs4_good2bad_hat hs4_good2disab_hat
        hs4_bad2exce_hat hs4_bad2good_hat hs4_bad2bad_hat hs4_bad2disab_hat
        lfpr_hs4diff_E2G_hat lfpr_hs4diff_G2B_hat lfpr_hs4diff_B2D_hat 
    using `lvdhs'/raw_health_CPS_hs.raw, clear;
drop if t<50 | t>80;
ren *_hat *;
sort t;
merge 1:1 t using ${gpicsdir}/dta_plot_health.dta, nogenerate;
sort t;
save ${gpicsdir}/dta_plot_health.dta, replace;
/*****/

use ${gpicsdir}/dta_plot_health.dta, clear;
local lvxlabel "xlabel(50(10)80,grid)";

line s_hs4exce s_hs4good s_hs4bad s_hs4disab t, 
    graphregion(color(white)) 
    lpattern("l" "_" "_." "-")  xtitle("Age") `lvxlabel'
    ytitle("Distribution of health status") yscale(range(0 1)) ylabel(0(0.2)1)
    legend(pos(11) ring(0) col(2) order(1 "Excellent" 2 "Good" 3 "Bad" 4 "Disabled"));
graph export ${gpicsdir}/dist_sim_col0_hs4.eps,replace;

line hs4exce hs4good hs4bad hs4disab t, 
    graphregion(color(white)) 
    lpattern("l" "_" "_." "-")  xtitle("Age") `lvxlabel'
    ytitle("Distribution of health status") yscale(range(0 1)) ylabel(0(0.2)1)
    legend(pos(11) ring(0) col(2) order(1 "Excellent" 2 "Good" 3 "Bad" 4 "Disabled"));
graph export ${gpicsdir}/dist_data_col0_hs4.eps,replace;

line s_hs4exce s_hs4good s_hs4bad s_hs4disab hs4exce hs4good hs4bad hs4disab t, 
    graphregion(color(white)) 
    lpattern("l" "_" "_-" "-" "_." "_-." "-." "l")  xtitle("Age") `lvxlabel'
    ytitle("Distribution of health status") yscale(range(0 1)) ylabel(0(0.2)1)
    legend(pos(11) ring(0) col(2) colfirst 
        order(1 "Sim: Excellent" 2 "Sim: Good" 3 "Sim: Bad" 4 "Sim: Disabled"
              5 "Excellent" 6 "Good" 7 "Bad" 8 "Disabled"));
graph export ${gpicsdir}/dist_compare_col0_hs4.eps,replace;

line s_hs4exce s_hs4good s_hs4bad s_hs4disab 
     hs4exce2_sim hs4good2_sim hs4bad2_sim hs4disab2_sim t, 
    graphregion(color(white)) 
    lpattern("l" "_" "_-" "-" "_." "_-." "-." "l")  xtitle("Age") `lvxlabel'
    ytitle("Distribution of health status") yscale(range(0 1)) ylabel(0(0.2)1)
    legend(pos(11) ring(0) col(2) colfirst 
        order(1 "Sim: Excellent" 2 "Sim: Good" 3 "Sim: Bad" 4 "Sim: Disabled"
              5 "v2: Excellent" 6 "v2: Good" 7 "v2: Bad" 8 "v2: Disabled"));
graph export ${gpicsdir}/dist_compare_v2_col0_hs4.eps,replace;

foreach ih in exce good bad {;
    line hs4_`ih'2exce hs4_`ih'2good hs4_`ih'2bad hs4_`ih'2disab t, 
        graphregion(color(white)) 
        lpattern("l" "_" "_." "-")  xtitle("Age") `lvxlabel'
        ytitle("Transition probability") yscale(range(0 1)) ylabel(0(0.2)1)
        legend(pos(11) ring(0) col(2) 
            order(1 "To excellent" 2 "To good" 3 "To bad" 4 "To disabled"));
    graph export ${gpicsdir}/trans_data_col0_hs4`ih'.eps,replace;

    line s_hs4_`ih'2exce s_hs4_`ih'2good s_hs4_`ih'2bad s_hs4_`ih'2disab
         hs4_`ih'2exce hs4_`ih'2good hs4_`ih'2bad hs4_`ih'2disab t, 
        graphregion(color(white)) 
        lpattern("l" "_" "_-" "-" "_." "_-." "-." "l") xtitle("Age") `lvxlabel'
        ytitle("Transition probability") yscale(range(0 1)) ylabel(0(0.2)1)
        legend(pos(11) ring(0) col(2) colfirst
           order(1 "Sim: To excellent" 2 "Sim: To good" 3 "Sim: To bad" 4 "Sim: To disabled"
                 5 "To excellent" 6 "To good" 7 "To bad" 8 "To disabled"));
    graph export ${gpicsdir}/trans_sim_col0_hs4`ih'.eps,replace;

    foreach iver in 0 1 2 3 {;
        line s_hs4_`ih'2exce s_hs4_`ih'2good s_hs4_`ih'2bad s_hs4_`ih'2disab
             hs4_`ih'2exce`iver'_hat hs4_`ih'2good`iver'_hat 
             hs4_`ih'2bad`iver'_hat hs4_`ih'2disab`iver'_hat t, 
            graphregion(color(white)) 
            lpattern("l" "_" "_-" "-" "_." "_-." "-." "l") xtitle("Age") `lvxlabel'
            ytitle("Transition probability") yscale(range(0 1)) ylabel(0(0.2)1)
            legend(pos(11) ring(0) col(2) colfirst
               order(1 "Sim: To excellent" 2 "Sim: To good" 3 "Sim: To bad" 4 "Sim: To disabled"
                     5 "v`iver': To excellent" 6 "v`iver': To good" 7 "v`iver': To bad" 8 "v`iver': To disabled"));
        graph export ${gpicsdir}/trans_sim_v`iver'_col0_hs4`ih'.eps,replace;
    }; 
};

cap log close;


